*-------------------------------------------------------------------------------------------------------------
*
*	PREPARE FILE
*
*-------------------------------------------------------------------------------------------------------------


set more off

qui{
*================= SETTINGS ======================================================================================
/* variable legend: _r - real
				    _gr - growth rate
					_d - differences
					_gap - gap measure (st - short-term, mt - mid-term
										bk - baxter-king, cf - christiano-fitzgerald)
					_y - to GDP ratio
					_fc - financial centre country
					_ba - base country
*/

* Variable types to generate
/* put spaces in beginning of locals for later subinstr() commands to work */
local iepolate_two "p_uwo_nor_index p_uwo_sou_index bay_imp p_wool nor_ewo_sac tol_can tol_clo tol_cer tol_fis tol_foo tol_the tol_bro w_shb_bar_fe wool_prod wool_prod2"
local gro " scont_bar_fe scont_val_fe p_uwo_ns_index p_uwo_nor_index p_uwo_sou_index p_uwo_nor_index_iep p_uwo_sou_index_iep exports_esp bay_imp bay_imp_iep w_agri_unskill_index w_otherservices_index w_relation_index w_master_index w_journey_index w_helper_index w_barshep_index w_jor_and1 w_jor_and2 w_and_index p_coarse_index p_fine_index p_threads_index p_textiles_nca p_egg_index p_hon_index p_che_index p_win_index p_fis_index p_nai_index essentials_index nonessentials_index processed_index p_wool nor_ewo_sac nor_ewo_sac_iep p_iron exp_iron tol_can tol_clo tol_cer tol_fis tol_foo tol_the tol_bro tol_can_iep tol_clo_iep tol_cer_iep tol_fis_iep tol_foo_iep tol_the_iep tol_bro_iep p_clo_index w_shb_bar_fe_iep w_shb_bar_fe p_barley_index p_wheat_index p_legumes_index p_mutton_index p_beef_index p_oliveoil_index p_candles_index p_soap_index p_charcoal_index p_firewood_index p_rice_index w_index_lz w_nca_rb cpi_nca_rb in_esp gdp_r_adj wool_prod wool_prod2 wool_prod_iep wool_prod2_iep cpi_uoa cpi_sil w_val_lz w_sev_lz w_mad_lz w_bar_lz w_dol_lz scont_sil scont_esp_al gs_rate salv perclossN perclossN_noco esp_rate sev_er cad_er mad_er bar_er sar_er val_er med_er gen_er ams_er ant_er lon_er par_er ham_er ven_er fra_er mil_er temperature sisto_esp wage_sil wage_uoa wage_uoa_r wage_sil_r n_occ w_weaver" /*growth rates*/ 
local cum "`gro'" /*cumulated growth*/
local diff " ships_bas percloss_salvN itsN itsN_plus pirate warbciv warbeu"
* horizon
local H 5

*=================================================================================================================


*============ PREPARE DATA =============

*_________ GET DATA ____________
use "data/working files/VagariesMoney.dta", clear


*____ DECLARE TIME SERIES _____
tsset year, yearly
gen ccode=1


drop disaster
gen disaster = 0
replace disaster = 1 if perclossN > 0


*____ SILVER CONTENT SCALING
replace scont_esp_al = 5*scont_esp_al // from gr. of silver / peseta to gr. of silver per peso (peso = 5 pesetas)
replace scont_esp_al = l.scont_esp_al if scont_esp_al ==. & l.scont_esp_al!=. //extent series to full sample
forvalues n = 1/10 {
	replace scont_esp_al = f.scont_esp_al if scont_esp_al ==. & f.scont_esp_al!=.
	}
	
	
// outlier year in Bayonne imports
replace bay_imp = . if year == 1759 //outlier

// remove non-annual bits from wool production series
replace wool_prod = . if year >= 1532 & year <= 1535


//remove earliest observations of basque shipproduction data, due to lower quality
replace ships_bas = . if year <= 1575 // problematic to adjust production to exact year due to lack of year info in primary sources (Oyarbide, 2002)



//___ from units of account to silver grams
// prices
foreach v in p_textiles_nca {
	replace `v' = `v'/272*scont_esp_al  //from maravedi/real unit to grams of silver/real unit
	}
foreach v in p_wool {
	replace `v' = `v'/272*scont_esp_al/11.5  //from maravedi/arroba to grams of silver/kg
	}
foreach v in p_cotton p_hemp p_twine  {
	replace `v' = `v'/272*scont_esp_al/0.45 //from maravedi/pound to grams of silver/kg
	}
foreach v in p_uwo_seg p_uwo_gua p_uwo_cas p_uwo_oce p_uwo_and { 
	replace `v' = `v'/8*scont_esp_al/11.5 //from reales/arroba to grams of silver/kg
	}


// exports
replace exports_esp = exports_esp/20*scont_esp_al //from reales de vellon to silver grams

// wages
local w_castile "w_blacksm_help w_blacksm_journ w_cabinet_help w_cabinet_journ w_carp_help w_carp_journ_nc w_laborer_nca w_mason_help_nca w_mason_journ_nc w_saw_marble w_saw_wood w_stonecut w_wheelwright w_basket w_book w_carp_journ_and w_cart w_cook w_laborer_oc w_laborer_nc w_laborer_and w_maid w_mason_mast_and w_nurse w_carp_mast_nc w_gard w_laund w_miller w_ward w_door w_dray w_steward w_carp"
foreach w of local w_castile {
	replace `w' = `w'/272*scont_esp_al //from maravedi to silver grams
}

local w_palencia "w_lab_pal_ml"
foreach w of local w_palencia {
	replace `w' = `w'/20*scont_esp_al //from reales de vellon to silver grams
}

local w_andalusia_reales "w_jor_and1 w_jor_and2"
foreach w of local w_andalusia_reales {
	replace `w' = `w'/20*scont_esp_al //from reales de vellon to silver grams
}


local w_barcelona "w_mbu_bar_fe w_hbu_bar_fe w_mca_bar_fe w_dla_bar_fe w_bsh_bar_fe w_csh_bar_fe w_shb_bar_fe w_jor_cat1 w_jor_cat2 w_jor_cat3 w_jor_catint w_jor_catlit"
foreach w of local w_barcelona {
	replace `w' = `w'*scont_bar_fe //from sou to silver grams
}

local w_valencia "w_carp_journ_val w_mason_journ_val w_mason_mast_val w_mason_help w_sacri w_stonecut_journ w_stonecut_mast w_weaver w_wetnurse w_carp_mast_val w_cook_val"
foreach w of local w_valencia {
	replace `w' = `w'/12*scont_val_fe //from diners to silver grams
}



	
// splice Hamilton/Allen and Drehlichman, 2009 wool price for Andalusia/Seville
gen jnk1 = p_wool if year == 1603
egen jnk2 = min(jnk1)
gen jnk3 = p_uwo_and if year == 1603
egen jnk4 = min(jnk3)
gen jnk5 = p_wool/jnk2 * jnk4
gen p_uwo_and_syn = p_uwo_and
replace p_uwo_and_syn = jnk5 if p_uwo_and_syn==. & jnk5!=.
drop jnk*


	
//iron price data after 1780 dominated by few buyers, and since early 1800s fixed due to tax fixed in monetary terms
replace p_iron = . if year >=1780 



//________ Törnquist indices

//Törnquist index based on Philips & Philips, 1997 (as digitized by Allen, 2001)


//wool prices south

local p_uwo "p_uwo_gua p_uwo_and_syn" //
local good
foreach w of local p_uwo {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_uwo_sou_index = rowmean(`good')

gen ln_p_uwo_sou_index = . 
replace ln_p_uwo_sou_index = 1 if year == 1558
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1558

replace ln_p_uwo_sou_index = l2.ln_p_uwo_sou_index + ( ln(p_uwo_and_syn) - ln(l2.p_uwo_and_syn) ) if year == 1568
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1568
replace ln_p_uwo_sou_index = l2.ln_p_uwo_sou_index + ( ln(p_uwo_and_syn) - ln(l2.p_uwo_and_syn) ) if year == 1571
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1571
replace ln_p_uwo_sou_index = l2.ln_p_uwo_sou_index + ( ln(p_uwo_and_syn) - ln(l2.p_uwo_and_syn) ) if year == 1575
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1575
replace ln_p_uwo_sou_index = l4.ln_p_uwo_sou_index + ( ln(p_uwo_and_syn) - ln(l4.p_uwo_and_syn) ) if year == 1624
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1624
replace ln_p_uwo_sou_index = l2.ln_p_uwo_sou_index + ( ln(p_uwo_and_syn) - ln(l2.p_uwo_and_syn) ) if year == 1629
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1629
replace ln_p_uwo_sou_index = l4.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l4.p_uwo_gua) ) if year == 1693
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1693
replace ln_p_uwo_sou_index = l5.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l5.p_uwo_gua) ) if year == 1704
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1704
replace ln_p_uwo_sou_index = l4.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l4.p_uwo_gua) ) if year == 1714
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1714
replace ln_p_uwo_sou_index = l4.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l4.p_uwo_gua) ) if year == 1721
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1721
replace ln_p_uwo_sou_index = l4.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l4.p_uwo_gua) ) if year == 1728
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1728
replace ln_p_uwo_sou_index = l5.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l5.p_uwo_gua) ) if year == 1735
replace ln_p_uwo_sou_index = l3.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l3.p_uwo_gua) ) if year == 1738
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1738
replace ln_p_uwo_sou_index = l15.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(l15.p_uwo_gua) ) if year == 1765
replace ln_p_uwo_sou_index = l.ln_p_uwo_sou_index + dln_p_uwo_sou_index if year > 1765

replace ln_p_uwo_sou_index = f.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(f.p_uwo_gua) ) if year == 1628

replace ln_p_uwo_sou_index = f.ln_p_uwo_sou_index + ( ln(p_uwo_gua) - ln(f.p_uwo_gua) ) if year == 1721

gen p_uwo_sou_index = exp(ln_p_uwo_sou_index)









//wool prices north
drop ln_* dln_*

local p_uwo "p_uwo_seg p_uwo_oce p_uwo_cas"  
local good
foreach w of local p_uwo {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_uwo_nor_index = rowmean(`good')

gen ln_p_uwo_nor_index = . 
replace ln_p_uwo_nor_index = 1 if year == 1603
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1603
replace ln_p_uwo_nor_index = l2.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l2.p_uwo_seg) ) if year == 1657
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1657
replace ln_p_uwo_nor_index = l5.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l5.p_uwo_seg) ) if year == 1713
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1713
replace ln_p_uwo_nor_index = l2.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l2.p_uwo_seg) ) if year == 1722
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1722
replace ln_p_uwo_nor_index = l2.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l2.p_uwo_seg) ) if year == 1743
replace ln_p_uwo_nor_index = l2.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l2.p_uwo_seg) ) if year == 1745
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1745
replace ln_p_uwo_nor_index = l5.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l5.p_uwo_seg) ) if year == 1755
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1755
forvalues y = 1754(-1)1752 {
	replace ln_p_uwo_nor_index = f.ln_p_uwo_nor_index - f.dln_p_uwo_nor_index if year == `y'
}
replace ln_p_uwo_nor_index = l2.ln_p_uwo_nor_index + ( ln(p_uwo_seg) - ln(l2.p_uwo_seg) ) if year == 1761
replace ln_p_uwo_nor_index = l.ln_p_uwo_nor_index + dln_p_uwo_nor_index if year > 1761

replace ln_p_uwo_nor_index = f.ln_p_uwo_nor_index + ( ln(p_uwo_cas) - ln(f.p_uwo_cas) ) if year == 1760

replace ln_p_uwo_nor_index = ln(p_uwo_seg) if year <= 1590

gen p_uwo_nor_index = exp(ln_p_uwo_nor_index)
order year p_uwo_nor_index ln_p_uwo_nor_index dln_p_uwo_nor_index p_uwo_seg p_uwo_cas p_uwo_oce






//wool prices north & south
drop ln_* dln_*

local p_uwo "p_uwo_nor_index p_uwo_sou_index" //p_uwo_cas 
local good
foreach w of local p_uwo {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_uwo_ns_index = rowmean(`good')

gen ln_p_uwo_ns_index = . 
replace ln_p_uwo_ns_index = 1 if year == 1558
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1558
replace ln_p_uwo_ns_index = l2.ln_p_uwo_ns_index + ( ln(p_uwo_sou_index) - ln(l2.p_uwo_sou_index) ) if year == 1568
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1568
replace ln_p_uwo_ns_index = l2.ln_p_uwo_ns_index + ( ln(p_uwo_sou_index) - ln(l2.p_uwo_sou_index) ) if year == 1571
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1571
replace ln_p_uwo_ns_index = l2.ln_p_uwo_ns_index + ( ln(p_uwo_sou_index) - ln(l2.p_uwo_sou_index) ) if year == 1575
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1575
replace ln_p_uwo_ns_index = l5.ln_p_uwo_ns_index + ( ln(p_uwo_nor_index) - ln(l5.p_uwo_nor_index) ) if year == 1713
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1713
replace ln_p_uwo_ns_index = l2.ln_p_uwo_ns_index + ( ln(p_uwo_nor_index) - ln(l2.p_uwo_nor_index) ) if year == 1722
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1722
replace ln_p_uwo_ns_index = l2.ln_p_uwo_ns_index + ( ln(p_uwo_nor_index) - ln(l2.p_uwo_nor_index) ) if year == 1752
replace ln_p_uwo_ns_index = l.ln_p_uwo_ns_index + dln_p_uwo_ns_index if year > 1752

replace ln_p_uwo_ns_index = f.ln_p_uwo_ns_index + ( ln(p_uwo_sou_index) - ln(f.p_uwo_sou_index) ) if year == 1721

replace ln_p_uwo_ns_index = ln(p_uwo_nor_index) if year <= 1557


gen p_uwo_ns_index = exp(ln_p_uwo_ns_index)



// Hamilton, 1947 textiles price indices
//coarse 
local p_textiles "p_burlap_nca p_sackcloth_nca p_grogram_nca" 
local good
foreach w of local p_textiles {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_coarse_index = rowmean(`good')

gen ln_p_coarse_index = . 
replace ln_p_coarse_index = 1 if year == 1651
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1651
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l2.p_burlap_nca) ) if year == 1714
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1714
replace ln_p_coarse_index = l3.ln_p_coarse_index + ( ln(p_sackcloth_nca) - ln(l3.p_sackcloth_nca) ) if year == 1716
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1716
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l2.p_burlap_nca) ) if year == 1752
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1752
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_sackcloth_nca) - ln(l2.p_sackcloth_nca) ) if year == 1753
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1753
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l2.p_burlap_nca) ) if year == 1781
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1781
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l2.p_burlap_nca) ) if year == 1791
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1791
replace ln_p_coarse_index = l5.ln_p_coarse_index + ( ln(p_sackcloth_nca) - ln(l5.p_sackcloth_nca) ) if year == 1792
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1792
replace ln_p_coarse_index = l5.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l5.p_burlap_nca) ) if year == 1796
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1796
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l2.p_burlap_nca) ) if year == 1798
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1798
replace ln_p_coarse_index = l2.ln_p_coarse_index + ( ln(p_burlap_nca) - ln(l2.p_burlap_nca) ) if year == 1800
replace ln_p_coarse_index = l.ln_p_coarse_index + dln_p_coarse_index if year > 1800


gen p_coarse_index = exp(ln_p_coarse_index)




//fine 
local p_textiles "p_whitecloth_nca p_serge_nca p_linen_nca" 
local good
foreach w of local p_textiles {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_fine_index = rowmean(`good')

gen ln_p_fine_index = . 
replace ln_p_fine_index = 1 if year == 1651
replace ln_p_fine_index = l.ln_p_fine_index + dln_p_fine_index if year > 1651
replace ln_p_fine_index = l2.ln_p_fine_index + 0.5* ( ln(p_whitecloth_nca) - ln(l2.p_whitecloth_nca) ) + 0.5* ( ln(p_serge_nca) - ln(l2.p_serge_nca) ) if year == 1772
replace ln_p_fine_index = l.ln_p_fine_index + dln_p_fine_index if year > 1772
replace ln_p_fine_index = l2.ln_p_fine_index + ( ln(p_whitecloth_nca) - ln(l2.p_whitecloth_nca) ) if year == 1780
replace ln_p_fine_index = l.ln_p_fine_index + dln_p_fine_index if year > 1780

gen p_fine_index = exp(ln_p_fine_index)



//threads 
local p_textiles "p_twine_nca p_blackthread_nca p_whitethread_nca p_yarn_nca" 
local good
foreach w of local p_textiles {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_threads_index = rowmean(`good')

gen ln_p_threads_index = . 
replace ln_p_threads_index = 1 if year == 1651
replace ln_p_threads_index = l.ln_p_threads_index + dln_p_threads_index if year > 1651

gen p_threads_index = exp(ln_p_threads_index)













*_________ DEFLATE ____________
*rename house price variable
n display "deflate variables"


*____ EXPORT TO GDP RATIO _____
gen gdp_nom_pesos = gdp_nom_reales*10^6/20
gen exports_esp_pesos = exports_esp*10^6/20
gen ety = exports_esp_pesos/gdp_nom_pesos // export-to-GDP ratio
n sum ety if year >=1531 & year <= 1810, d
n display `"Export to GDP ratio, high. mean: `r(mean)', min: `r(min)', max: `r(max)' "'


*_________ INTRA- & EXTRAPOLATE FLOWS

*set initial values for 1492

*linear interpolation
n display "inter- and extrapolate"


local vecsize : word count `iepolate_two'

forvalues x = 1/`vecsize'{
	local w : word `x' of `iepolate_two'

	tsspell, cond(`w'==.) end(`w'_pend) seq(`w'_pseq) spell(`w'_pspell) /*find spells*/
	egen hmseq_`w' = max(`w'_pseq), by(`w'_pspell) /*determine length of each spell*/

	ipolate `w' year, generate(`w'_iep)
	replace `w'_iep = . if hmseq_`w' > 1 // only interpolate short spells of missings
	order hmseq* *_pspell *pseq `w' `w'_iep
	
}
drop *pend *pseq *pspell hmseq*






*____ OUTPUT

//IPG method:
gen gdp_r_adj = output_r_ind // use annual part of Prados de la Escosura & Alvarez-Nogal GDP series for Spain





*_________ GENERATE WAGE SERIES ____________
gen wage_sil = wage_sil_r*cpi_sil
gen wage_uoa = wage_sil * 1/scont_esp_al // silver gram index -> maravedi index
gen cpi_uoa = cpi_sil * 1/scont_esp_al 
gen wage_uoa_r = wage_uoa/cpi_uoa


*_____ Törnquist Indices




//Agricultural wages - Andalusia (Ponsot, 1986)
local jobs "w_jor_and1 w_jor_and2" 
local occu
foreach w of local jobs {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_w_and_index = rowmean(`occu')

gen ln_w_and_index = . 

replace ln_w_and_index = 1 if year == 1700
replace ln_w_and_index = l.ln_w_and_index + dln_w_and_index if year > 1700
replace ln_w_and_index = l3.ln_w_and_index + ( ln(w_jor_and2) - ln(l3.w_jor_and2) ) if year == 1806 
replace ln_w_and_index = l.ln_w_and_index + dln_w_and_index if year > 1806

gen w_and_index = exp(ln_w_and_index)

drop *_w_and_index *_w_jor_and*



//Agricultural wages - Barcelona day laborers (Feliu, 1991; Garrabou & Tello, 2001)
gen w_bar_agr_dla_index = w_dla_bar_fe 
replace w_bar_agr_dla_index = w_jor_cat1 if year >= 1720
gen ratioo = w_jor_cat1 / w_dla_bar_fe 
sum ratioo if year == 1721
gen jnk1 = w_dla_bar_fe * `r(mean)'
replace w_bar_agr_dla_index = jnk1 if year < 1720

drop jnk1 ratioo



//Agricultural wages - Catalunia (Feliu, 1991; Garrabou & Tello, 2001)
local jobs "w_bar_agr_dla_index w_jor_cat2 w_jor_cat3" 
local occu
foreach w of local jobs {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_w_cat_index = rowmean(`occu')

gen ln_w_cat_index = . 
replace ln_w_cat_index = 1 if year == 1720
replace ln_w_cat_index = l.ln_w_cat_index + dln_w_cat_index if year > 1720
replace ln_w_cat_index = l4.ln_w_cat_index + ( ln(w_jor_catlit) - ln(l4.w_jor_catlit) ) if year == 1729
replace ln_w_cat_index = l.ln_w_cat_index + dln_w_cat_index if year > 1729
replace ln_w_cat_index = l4.ln_w_cat_index + ( ln(w_jor_catlit) - ln(l4.w_jor_catlit) ) if year == 1754
replace ln_w_cat_index = l.ln_w_cat_index + dln_w_cat_index if year > 1754
replace ln_w_cat_index = l2.ln_w_cat_index + ( ln(w_jor_catlit) - ln(l2.w_jor_catlit) ) if year == 1761
replace ln_w_cat_index = l.ln_w_cat_index + dln_w_cat_index if year > 1761


replace ln_w_cat_index = f3.ln_w_cat_index - ( ln(f3.w_jor_catint) - ln(w_jor_catint)) if year == 1753
replace ln_w_cat_index = f.ln_w_cat_index - ( ln(f.w_jor_catint) - ln(w_jor_catint)) if year == 1752
replace ln_w_cat_index = f.ln_w_cat_index - ( ln(f.w_jor_catint) - ln(w_jor_catint)) if year == 1728
replace ln_w_cat_index = f.ln_w_cat_index - ( ln(f.w_jor_catint) - ln(w_jor_catint)) if year == 1727


gen w_cat_index = exp(ln_w_cat_index)
gen jnk1 = w_cat_index / w_bar_agr_dla_index 
sum jnk1 if year == 1720
gen jnk2 = w_bar_agr_dla_index * `r(mean)'
replace w_cat_index = jnk2 if year < 1720
drop jnk*
drop *_w_cat_index *_w_bar_agr_dla_index









//___ Wage aggregates


//Agriculture - unskilled
drop ln_* dln_*

local agg "w_bar_agr_dla_index w_jor_cat2 w_jor_cat3 w_jor_and2 w_lab_pal_ml w_shb_bar_fe" 
local occu
foreach w of local agg {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_w_agri_unskill_index = rowmean(`occu')


gen ln_w_agri_unskill_index = . 
replace ln_w_agri_unskill_index = 1 if year == 1678
replace ln_w_agri_unskill_index = l.ln_w_agri_unskill_index + dln_w_agri_unskill_index if year > 1678
replace ln_w_agri_unskill_index = l2.ln_w_agri_unskill_index + ( ln(w_shb_bar_fe) - ln(l2.w_shb_bar_fe) ) if year == 1694
replace ln_w_agri_unskill_index = l.ln_w_agri_unskill_index + dln_w_agri_unskill_index if year > 1694




gen w_agri_unskill_index = exp(ln_w_agri_unskill_index)
gen jnk1 = w_agri_unskill_index / w_cat_index 
sum jnk1 if year == 1684 
gen jnk2 = w_cat_index * `r(mean)'
replace w_agri_unskill_index = jnk2 if year < 1678 // use w_cat_index which is composed of a narrower set of indicators that are available for the period prior to 1678
drop jnk*






//Shepherding wages - Barcelona (Feliu, 1991)
drop ln_* dln_*
local jobs "w_bsh_bar_fe w_csh_bar_fe w_shb_bar_fe" 
local occu
foreach w of local jobs {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_w_barshep_index = rowmean(`occu')

gen ln_w_barshep_index = . 
replace ln_w_barshep_index = 1 if year == 1545
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1545
replace ln_w_barshep_index = l27.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l27.w_csh_bar_fe) ) if year == 1573
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1573
replace ln_w_barshep_index = l7.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l7.w_csh_bar_fe) ) if year == 1581
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1581
replace ln_w_barshep_index = l4.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l4.w_csh_bar_fe) ) if year == 1585
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1585
replace ln_w_barshep_index = l4.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l4.w_csh_bar_fe) ) if year == 1589
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1589
replace ln_w_barshep_index = l2.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l2.w_csh_bar_fe) ) if year == 1598
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1598

replace ln_w_barshep_index = l3.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l3.w_csh_bar_fe) ) if year == 1601
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1601
replace ln_w_barshep_index = l2.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l2.w_csh_bar_fe) ) if year == 1603
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1603
replace ln_w_barshep_index = l4.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l4.w_csh_bar_fe) ) if year == 1610
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1610
replace ln_w_barshep_index = l13.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l13.w_csh_bar_fe) ) if year == 1624
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1624
replace ln_w_barshep_index = l2.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l2.w_csh_bar_fe) ) if year == 1626
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1626
replace ln_w_barshep_index = l3.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l3.w_csh_bar_fe) ) if year == 1629
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1629
replace ln_w_barshep_index = l2.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l2.w_csh_bar_fe) ) if year == 1631
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1631
replace ln_w_barshep_index = l4.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l4.w_csh_bar_fe) ) if year == 1643
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1643
replace ln_w_barshep_index = l7.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l7.w_csh_bar_fe) ) if year == 1668
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1668
replace ln_w_barshep_index = l3.ln_w_barshep_index + ( ln(w_csh_bar_fe) - ln(l3.w_csh_bar_fe) ) if year == 1673
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1673
replace ln_w_barshep_index = l2.ln_w_barshep_index + 0.5*( ln(w_csh_bar_fe) - ln(l2.w_csh_bar_fe) ) + 0.5*( ln(w_shb_bar_fe) - ln(l2.w_shb_bar_fe) ) if year == 1706
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1706
replace ln_w_barshep_index = l11.ln_w_barshep_index + 0.5*( ln(w_csh_bar_fe) - ln(l11.w_csh_bar_fe) ) +  0.5*( ln(w_shb_bar_fe) - ln(l11.w_shb_bar_fe) ) if year == 1799
replace ln_w_barshep_index = l.ln_w_barshep_index + dln_w_barshep_index if year > 1799

gen w_barshep_index = exp(ln_w_barshep_index)
drop *_w_bsh_bar_fe *_w_csh_bar_fe *_w_shb_bar_fe *_w_barshep_index





// Industry - masters
local agg "w_mbu_bar_fe w_mca_bar_fe w_mason_mast_val w_mason_mast_and w_stonecut_mast w_carp_mast_val w_carp_mast_nc" 
local occu
foreach w of local agg {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
	}

egen dln_w_master_index = rowmean(`occu')

gen ln_w_master_index = . 
replace ln_w_master_index = 1 if year == 1501
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1501
replace ln_w_master_index = l2.ln_w_master_index + 0.5*( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) + 0.5* ( ln(w_mason_mast_val) - ln(l2.w_mason_mast_val) )  if year == 1514
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1514
replace ln_w_master_index = l2.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) if year == 1633
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1633
replace ln_w_master_index = l2.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) if year == 1645
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1645
replace ln_w_master_index = l4.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l4.w_mbu_bar_fe) ) if year == 1665
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1665
replace ln_w_master_index = l5.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l5.w_mbu_bar_fe) ) if year == 1672
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1672
replace ln_w_master_index = l3.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l3.w_mbu_bar_fe) ) if year == 1675
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1675
replace ln_w_master_index = l3.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l3.w_mbu_bar_fe) ) if year == 1678
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1678
replace ln_w_master_index = l6.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l6.w_mbu_bar_fe) ) if year == 1684
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1684
replace ln_w_master_index = l2.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) if year == 1687
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1687
replace ln_w_master_index = l2.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) if year == 1692
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1692
replace ln_w_master_index = l2.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) if year == 1696
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1696
replace ln_w_master_index = l2.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l2.w_mbu_bar_fe) ) if year == 1701
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1701
replace ln_w_master_index = l5.ln_w_master_index + ( ln(w_mbu_bar_fe) - ln(l5.w_mbu_bar_fe) ) if year == 1745
replace ln_w_master_index = l.ln_w_master_index + dln_w_master_index if year > 1745

replace ln_w_master_index = f.ln_w_master_index + ( ln(w_mason_mast_and) - ln(f.w_mason_mast_and) ) if year == 1632
replace ln_w_master_index = f.ln_w_master_index + ( ln(w_mason_mast_and) - ln(f.w_mason_mast_and) ) if year == 1644
replace ln_w_master_index = f3.ln_w_master_index + ( ln(w_mca_bar_fe) - ln(f3.w_mca_bar_fe) ) if year == 1664
replace ln_w_master_index = f.ln_w_master_index + ( ln(w_mca_bar_fe) - ln(f.w_mca_bar_fe) ) if year == 1686
replace ln_w_master_index = f10.ln_w_master_index + ( ln(w_mca_bar_fe) - ln(f10.w_mca_bar_fe) ) if year == 1676
replace ln_w_master_index = f.ln_w_master_index + ( ln(w_mca_bar_fe) - ln(f.w_mca_bar_fe) ) if year == 1700

gen w_master_index = exp(ln_w_master_index)
drop *_w_mbu_bar_fe *_w_mca_bar_fe *_w_mason_mast_and



// Industry - journeymen
local agg "w_blacksm_journ w_cabinet_journ w_carp_journ_nc w_mason_journ_nc w_stonecut w_wheelwright w_carp_journ_val w_carp_journ_and w_mason_journ_val w_stonecut_journ" 
local occu
foreach w of local agg {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
	}

egen dln_w_journey_index = rowmean(`occu')

gen ln_w_journey_index = . 
replace ln_w_journey_index = 1 if year == 1502
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1502
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_stonecut_journ) - ln(l2.w_stonecut_journ) ) if year == 1508
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1508
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_mason_journ_val) - ln(l2.w_mason_journ_val) ) if year == 1514
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1514
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_mason_journ_val) - ln(l2.w_mason_journ_val) ) if year == 1532
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1532
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_val) - ln(l2.w_carp_journ_val) ) if year == 1574
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1574
replace ln_w_journey_index = l.ln_w_journey_index + 0 if year == 1601 // Allen's 2001 Valencia journeymen wage series shows constant wages
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1601
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l2.w_carp_journ_and) ) if year == 1611
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1611
replace ln_w_journey_index = l3.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l3.w_carp_journ_and) ) if year == 1614
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1614
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l2.w_carp_journ_and) ) if year == 1616
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1616
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l2.w_carp_journ_and) ) if year == 1618
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1618
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l2.w_carp_journ_and) ) if year == 1620
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1620
replace ln_w_journey_index = l3.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l3.w_carp_journ_and) ) if year == 1623
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1623
replace ln_w_journey_index = l3.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l3.w_carp_journ_and) ) if year == 1632
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1632
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l2.w_carp_journ_and) ) if year == 1636
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1636
replace ln_w_journey_index = l4.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l4.w_carp_journ_and) ) if year == 1641
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1641
replace ln_w_journey_index = l3.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l3.w_carp_journ_and) ) if year == 1646
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1646
replace ln_w_journey_index = l2.ln_w_journey_index + ( ln(w_carp_journ_and) - ln(l2.w_carp_journ_and) ) if year == 1650
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1650

replace ln_w_journey_index = 1 if year == 1737
replace ln_w_journey_index = l.ln_w_journey_index + dln_w_journey_index if year > 1737

replace ln_w_journey_index = f.ln_w_journey_index + ( ln(w_carp_journ_val) - ln(f.w_carp_journ_val) ) if year == 1513
replace ln_w_journey_index = f.ln_w_journey_index + ( ln(w_carp_journ_val) - ln(f.w_carp_journ_val) ) if year == 1531
replace ln_w_journey_index = f.ln_w_journey_index + ( ln(w_mason_journ_val) - ln(f.w_mason_journ_val) ) if year == 1573

gen w_journey_index = exp(ln_w_journey_index)





// Industry - helper
local agg "w_hbu_bar_fe w_blacksm_help w_cabinet_help w_carp_help w_mason_help_nca w_mason_help" 
local occu
foreach w of local agg {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
	}

egen dln_w_helper_index = rowmean(`occu')

gen ln_w_helper_index = . 
replace ln_w_helper_index = 1 if year == 1502
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1502
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_mason_help) - ln(l2.w_mason_help) ) if year == 1508
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1508
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_mason_help) - ln(l2.w_mason_help) ) if year == 1514
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1514
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_mason_help) - ln(l2.w_mason_help) ) if year == 1532
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1532
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_mason_help) - ln(l2.w_mason_help) ) if year == 1623
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1623
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1640
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1640
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1645
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1645
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1659
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1661
replace ln_w_helper_index = l4.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l4.w_hbu_bar_fe) ) if year == 1665
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1665
replace ln_w_helper_index = l5.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l5.w_hbu_bar_fe) ) if year == 1672
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1672
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1675
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1675
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1678
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1680
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1682
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1682
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1687
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1687
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1692
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1692
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1697
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1697
replace ln_w_helper_index = l4.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l4.w_hbu_bar_fe) ) if year == 1764
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1764
replace ln_w_helper_index = l2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(l2.w_hbu_bar_fe) ) if year == 1769
replace ln_w_helper_index = l.ln_w_helper_index + dln_w_helper_index if year > 1769

replace ln_w_helper_index = f.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(f.w_hbu_bar_fe) ) if year == 1507
replace ln_w_helper_index = f2.ln_w_helper_index + ( ln(w_hbu_bar_fe) - ln(f2.w_hbu_bar_fe) ) if year == 1501

gen w_helper_index = exp(ln_w_helper_index)
drop *_w_hbu_bar_fe







// Service - Relationship-specific
local agg "w_sacri w_book w_ward w_door w_steward" // 
local occu
foreach w of local agg {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
	}

egen dln_w_relation_index = rowmean(`occu')

gen ln_w_relation_index = . 
replace ln_w_relation_index = 1 if year == 1502
replace ln_w_relation_index = l.ln_w_relation_index + dln_w_relation_index if year > 1502
replace ln_w_relation_index = l.ln_w_relation_index  if year == 1551 // constant
replace ln_w_relation_index = l.ln_w_relation_index + dln_w_relation_index if year > 1551
replace ln_w_relation_index = l.ln_w_relation_index  if year == 1601 // constant
replace ln_w_relation_index = l.ln_w_relation_index + dln_w_relation_index if year > 1601

gen w_relation_index = exp(ln_w_relation_index)
order year w_relation_index ln_w_relation_index dln_w_relation_index w_sacri w_book w_maid w_ward w_door w_steward




// Service - Other services
drop ln_* dln_*
local agg "w_nurse w_wetnurse w_laund w_dray w_basket w_cart w_gard w_cook w_cook_val w_maid" // w_gard w_cook w_cook_val w_maid
local occu
foreach w of local agg {
	local occu "`occu' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
	}

egen dln_w_otherservices_index = rowmean(`occu')

gen ln_w_otherservices_index = . 
replace ln_w_otherservices_index = 1 if year == 1502
replace ln_w_otherservices_index = l.ln_w_otherservices_index + dln_w_otherservices_index if year > 1502

gen w_otherservices_index = exp(ln_w_otherservices_index)
order year w_otherservices_index ln_w_otherservices_index dln_w_otherservices_index w_nurse w_gard w_laund w_wetnurse w_sacri w_cook w_cook_val




























//Törnquist index based on Losa & Zarauz, 2021

//wages
foreach w in w_val_lz w_sev_lz w_mad_lz w_bar_lz w_dol_lz {
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

gen dln_w_index = 1/5*(dln_w_val_lz + dln_w_sev_lz + dln_w_mad_lz + dln_w_bar_lz + dln_w_dol_lz)

gen ln_w_index_lz = 1 if year == 1501
replace ln_w_index_lz = l.ln_w_index_lz + dln_w_index if year > 1501

gen w_index_lz = exp(ln_w_index_lz)



	
	
	

//disaggregated prices
foreach p in barley	wheat legumes mutton beef oliveoil linen candles soap charcoal firewood rice {
	local cities " "
	foreach c in val sev mad bar dol {
		sum p_`c'_`p'
		if (`r(N)' != 0) {
			local cities "`cities' `c' "
			gen ln_p_`c'_`p' = ln(p_`c'_`p')
			gen dln_p_`c'_`p' = ln_p_`c'_`p' - l.ln_p_`c'_`p'
			}
		} //cities
		
		local n_cities : word count `cities'
		
		local toern ""
		forvalues z	= 1/`n_cities'{
			local c`z' : word `z' of `cities'
			local toern "`toern'dln_p_`c`z''_`p' "
		 }
		
		local toernsum = subinstr("`toern'"," ","+",`n_cities'-1)
		

	gen dln_p_`p'_index = 1/`n_cities'*(`toernsum')
		
	gen ln_p_`p'_index = 1 if year == 1501
	replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1501

	gen p_`p'_index = exp(ln_p_`p'_index)
	order p_`p'_index
	} //product
	
	
	
foreach p in egg { 
	local cities " "
	foreach c in val sev mad bar dol {
		sum p_`c'_`p'
		if (`r(N)' != 0) {
			local cities "`cities' dln_p_`c'_`p'"
			gen ln_p_`c'_`p' = ln(p_`c'_`p')
			gen dln_p_`c'_`p' = ln_p_`c'_`p' - l.ln_p_`c'_`p'
			}
		} //cities
		
		egen dln_p_`p'_index = rowmean(`cities')

		gen ln_p_`p'_index = . 
		replace ln_p_`p'_index = 1 if year == 1502
		replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1502
		replace ln_p_`p'_index = l3.ln_p_`p'_index + (ln_p_dol_`p' - l3.ln_p_dol_`p') if year == 1506
		replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1506
		replace ln_p_`p'_index = l2.ln_p_`p'_index + (ln_p_dol_`p' - l2.ln_p_dol_`p') if year == 1510
		replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1510
		
		replace ln_p_`p'_index = f.ln_p_`p'_index + ( ln(p_val_`p') - ln(f.p_val_`p') ) if year == 1505
		replace ln_p_`p'_index = f.ln_p_`p'_index + ( ln(p_val_`p') - ln(f.p_val_`p') ) if year == 1504

		gen p_`p'_index = exp(ln_p_`p'_index)
	} //product
	
	
	
	
foreach p in hon { 
	local cities " "
	foreach c in val mad { // available for two cities
		sum p_`c'_`p'
		if (`r(N)' != 0) {
			local cities "`cities' dln_p_`c'_`p'"
			gen ln_p_`c'_`p' = ln(p_`c'_`p')
			gen dln_p_`c'_`p' = ln_p_`c'_`p' - l.ln_p_`c'_`p'
			}
		} //cities
		
		egen dln_p_`p'_index = rowmean(`cities')

		gen ln_p_`p'_index = . 
		replace ln_p_`p'_index = 1 if year == 1512
		replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1512
		
		gen p_`p'_index = exp(ln_p_`p'_index)
	} //product
	
	
	
// cheese price index (cheese prices available for two cities)
	gen p_che_index = p_mad_che 
	replace p_che_index = p_val_che if year >=1602 & year <=1650	
	replace p_che_index = l2.p_che_index * p_val_che/l2.p_val_che if year ==1534
	replace p_che_index = l.p_che_index*p_val_che/l.p_val_che if p_che_index == . & p_val_che != . & l.p_val_che != .
	
	forvalues k=1/7{
	replace p_che_index = f.p_che_index * p_val_che/f.p_val_che if p_che_index ==. & f.p_che_index !=. & p_val_che !=. & f.p_val_che !=.
}



foreach p in win { 
	local cities " "
	foreach c in val sev mad bar dol {
		sum p_`c'_`p'
		if (`r(N)' != 0) {
			local cities "`cities' dln_p_`c'_`p'"
			gen ln_p_`c'_`p' = ln(p_`c'_`p')
			gen dln_p_`c'_`p' = ln_p_`c'_`p' - l.ln_p_`c'_`p'
			}
		} //cities
		
		egen dln_p_`p'_index = rowmean(`cities')

		gen ln_p_`p'_index = . 
		replace ln_p_`p'_index = 1 if year == 1492
		replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1492
		replace ln_p_`p'_index = l2.ln_p_`p'_index + (ln_p_bar_`p' - l2.ln_p_bar_`p') if year == 1801
		replace ln_p_`p'_index = l.ln_p_`p'_index + dln_p_`p'_index if year > 1801
		replace ln_p_`p'_index = l2.ln_p_`p'_index + (ln_p_bar_`p' - l2.ln_p_bar_`p') if year == 1807

		gen p_`p'_index = exp(ln_p_`p'_index)
	} //product
	

	
// fish and sardine series (two series for Madrid)
gen p_fis_index = p_mad_fis
replace p_fis_index = p_mad_sar if year >= 1751

	
	
// nails price index (available for two cities)
	gen p_nai_index = p_val_nai 	
	replace p_nai_index = l.p_nai_index*p_dol_nai/l.p_dol_nai if p_nai_index == . & p_dol_nai != . & l.p_dol_nai != .
		replace p_nai_index = f9.p_nai_index * p_dol_nai/f9.p_dol_nai if year == 1503
		replace p_nai_index = f4.p_nai_index * p_dol_nai/f4.p_dol_nai if year == 1534
		replace p_nai_index = f4.p_nai_index * p_dol_nai/f4.p_dol_nai if year == 1614

	forvalues k=1/7{
	replace p_nai_index = f.p_nai_index * p_dol_nai/f.p_dol_nai if p_nai_index ==. & f.p_nai_index !=. & p_dol_nai !=. & f.p_dol_nai !=.
	}

	
 
//foodstuffs - grains & vegetables; essential
local p_essentials "p_barley_index p_wheat_index p_rice_index p_legumes_index" 
local good
foreach w of local p_essentials {
	local good "`good' dln_`w'"
	//gen ln_`w' = ln(`w')
	//gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_essentials_index = rowmean(`good')

gen ln_essentials_index = . 
replace ln_essentials_index = 1 if year == 1501
replace ln_essentials_index = l.ln_essentials_index + dln_essentials_index if year > 1501

gen essentials_index = exp(ln_essentials_index)



//foodstuffs - cereals
drop *ln_p_mad_barley *ln_p_mad_wheat 
local p_cereals "p_mad_barley p_mad_wheat p_mad_rice"  
local good
foreach w of local p_cereals {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_mad_cer_index = rowmean(`good')

gen ln_p_mad_cer_index = . 
replace ln_p_mad_cer_index = 1 if year == 1501
replace ln_p_mad_cer_index = l.ln_p_mad_cer_index + dln_p_mad_cer_index if year > 1501

gen p_mad_cer_index = exp(ln_p_mad_cer_index)



//foodstuffs - animal products; nonessentials 
drop *ln_p_mutton_index *ln_p_beef_index *ln_p_egg_index *ln_p_hon_index
local p_nonessentials "p_mutton_index p_beef_index p_egg_index p_fis_index p_hon_index" 
local good
foreach w of local p_nonessentials {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_nonessentials_index = rowmean(`good')

gen ln_nonessentials_index = . 
replace ln_nonessentials_index = 1 if year == 1501
replace ln_nonessentials_index = l.ln_nonessentials_index + dln_nonessentials_index if year > 1501

gen nonessentials_index = exp(ln_nonessentials_index)




//foodstuffs - processed 
drop *ln_p_win_index
local p_processed "p_win_index p_che_index" 
local good
foreach w of local p_processed {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_processed_index = rowmean(`good')

gen ln_processed_index = . 
replace ln_processed_index = 1 if year == 1501
replace ln_processed_index = l.ln_processed_index + dln_processed_index if year > 1501

gen processed_index = exp(ln_processed_index)




//cloth prices 
drop *ln_p_linen_index
local p_clo "p_cotton p_hemp p_twine p_linen_index" 
local good
foreach w of local p_clo {
	local good "`good' dln_`w'"
	gen ln_`w' = ln(`w')
	gen dln_`w' = ln_`w' - l.ln_`w'
}

egen dln_p_clo_index = rowmean(`good')

gen ln_p_clo_index = . 
replace ln_p_clo_index = 1 if year == 1501
replace ln_p_clo_index = l.ln_p_clo_index + dln_p_clo_index if year > 1501

gen p_clo_index = exp(ln_p_clo_index)



// deflate toledo nominal tax data
replace tol_can = tol_can/272*scont_esp_al/p_clo_index // historically, coarse canvas textiles was made from hemp (Hemp price data from Allen, originally Hamilton)
replace tol_clo = tol_clo/272*scont_esp_al/p_clo_index // combines LZ linen w/ cotton, hemp, and twine price data from Allen/originally Hamilton
replace tol_cer = tol_cer/272*scont_esp_al/p_mad_cer_index // LZ wheat and barley price index for Madrid
replace tol_foo = tol_foo/272*scont_esp_al/cpi_nca_rb // LZ CPI; alternative: New castile cpi (Reher & Ballesteros, 1993: cpi_nca_rb) Spain-wide CPI, as Madrid can be specific accordign to recent Madrid price paper (court effect) ; p_mad_index is LZ, 2021 madrid food items index, equiweighted; similar results
replace tol_the_iep = tol_the_iep/272*scont_esp_al/w_nca_rb // skilled (wage_sil) vs unskilled (w_index_lz) (PA, 2013 vs. LZ, 2021); region: Madrid, New Castile (w_nca_rb)
replace tol_bro_iep = tol_bro_iep/272*scont_esp_al/w_nca_rb 
replace tol_fis = tol_fis/p_fis_index // Madrid fish price index 







*_________ INTEREST RATES ____________


foreach r in esp_rate ams_lon_ss gs_rate sev_er cad_er mad_er bar_er sar_er val_er med_er gen_er ams_er ant_er lon_er par_er ham_er ven_er fra_er mil_er {
	replace `r' = 0.5*l.`r' + 0.5*f.`r' if `r'==. // linearly interpolate
	}	
foreach r in sev cad mad bar sar val med gen ams ant lon par ham ven fra mil {
	replace `r'_mat = 0.5*l.`r'_mat + 0.5*f.`r'_mat if `r'_mat==. // linearly interpolate
	replace `r'_gs = 0.5*l.`r'_gs + 0.5*f.`r'_gs if `r'_gs==. 
	}


// correct interpolation for jumps in esp_rate/re-introduce deliberate missings due to city-pair change that was interpolated in previous step (due to source breaks/ level shift due to city - pair change)
replace esp_rate = . if year == 1575 | year == 1700 | year == 1708 | year == 1709 | year == 1723 | year == 1724| year == 1725 | year == 1726 | year == 1727  

// silver content per maravedi (splice Allen's 'grams of silver per maravedis' series and Kivanc et al's 'grams of silver per vellon maravedis' for the post vellon debasement years to get long-run silver content of silver coins series)
replace scont_sil = l.scont_sil * scont_esp_al/l.scont_esp_al if scont_sil ==.


//logs
foreach r in esp_rate scont_sil ams_lon_ss gs_rate { 
	gen l_`r' = log(`r')
	}


local det l_esp_rate l_scont_sil l_ams_lon_ss l_gs_rate 
include code/detrend_hp.do

//replace l_scont_sil_hp = l_pesograms_hp //0 if year > 1709 // no more silver content changes after 1709

foreach r in sev cad mad bar sar val med {	
	gen l_`r'_er = ln(`r'_er)
	local det l_`r'_er
	include code/detrend_hp.do

	gen log_R_`r'_er_hp_base = -4*l_`r'_er_hp*100 
	
	gen log_R_`r'_er_hp_base2 = log_R_`r'_er_hp_base
	replace log_R_`r'_er_hp_base2 = log_R_`r'_er_hp_base2 - 12/`r'_mat*l_gs_rate_hp*100 + l_scont_sil_hp*100 if `r'_gs == 2 & log_R_`r'_er_hp_base !=. //foreign: gold; home: silver; account for changes in silver content of Spanish silver currency which might be correlated with the shock
	}
	
	
foreach r in lon gen ams ant par ham ven fra mil {	
	gen l_`r'_er = ln(`r'_er)
	local det l_`r'_er
	include code/detrend_hp.do

	gen log_R_`r'_er_hp_base = -4*l_`r'_er_hp*100 
	
	gen log_R_`r'_er_hp_base2 = log_R_`r'_er_hp_base
	replace log_R_`r'_er_hp_base2 = log_R_`r'_er_hp_base2 - 12/`r'_mat*l_gs_rate_hp*100 if `r'_gs == 2 & log_R_`r'_er_hp_base !=. //foreign: gold; home: silver (does not account for changes in metal content of foreign currencies, which is less likely to be correlated with Spanish silver inflow shock)
	replace log_R_`r'_er_hp_base2 = log_R_`r'_er_hp_base2 + 12/`r'_mat*l_gs_rate_hp*100 if `r'_gs == 3 & log_R_`r'_er_hp_base !=. //foreign: silver; home: gold
	}


	
foreach r in lon sev cad mad bar sar val med gen ams ant par ham ven fra mil {	
	gen log_R_`r'_er_hp = log_R_`r'_er_hp_base2
	}


foreach r in l_esp_rate  {
	replace `r'_hp = 100*`r'_hp 
	}

//GS rate adjustment
replace l_esp_rate_hp = l_esp_rate_hp + l_gs_rate_hp*100 if year >=1563 & year <=1596
replace l_esp_rate_hp = l_esp_rate_hp - l_gs_rate_hp*100 if year >=1717 & year <=1810

//annualize esp_rate 
replace l_esp_rate_hp = l_esp_rate_hp*12 //note: no '-12', because the ratio of bills for the NER cancellation method already takes this into account


//interpolate
tsspell, cond(l_esp_rate_hp==.) end(l_esp_rate_hp_pend) seq(l_esp_rate_hp_pseq) spell(l_esp_rate_hp_pspell) /*find spells*/
	egen hmseq_l_esp_rate_hp = max(l_esp_rate_hp_pseq), by(l_esp_rate_hp_pspell) /*determine length of each spell*/

	ipolate l_esp_rate_hp year, generate(l_esp_rate_hp_iep)
	replace l_esp_rate_hp_iep = . if hmseq_l_esp_rate_hp >= 5 // only interpolate short spells of missings
	drop hmseq* *_pspell *pseq //l_esp_rate_hp l_esp_rate_hp_iep
	














	//Spanish lending rate (average of Spanish cities)
	egen num = rowtotal(log_R_sev_er_hp log_R_sar_er_hp log_R_cad_er_hp log_R_mad_er_hp log_R_bar_er_hp log_R_val_er_hp log_R_med_er_hp), missing // log_R_bar_er_hp log_R_val_er_hp
	egen denom = rownonmiss(log_R_sev_er_hp log_R_sar_er_hp log_R_cad_er_hp log_R_mad_er_hp log_R_bar_er_hp log_R_val_er_hp log_R_med_er_hp) // log_R_sar_er_hp log_R_bar_er_hp log_R_val_er_hp
	replace denom = . if denom==0
	gen log_R_esp_er_hp = num/denom
	

	gen log_R_esp_er_hp_hi = 1/(1-0.25)*log_R_esp_er_hp // 2 weeks travel and 2 month bills
	gen log_R_esp_er_hp_lo = 1/(1+0.25)*log_R_esp_er_hp // 2 weeks travel and 2 month bills
	gen log_R_esp_er_hp_hi4w = 1/(1-0.5)*log_R_esp_er_hp // 4 weeks travel and 2 month bills
	gen log_R_esp_er_hp_lo4w = 1/(1+0.5)*log_R_esp_er_hp // 4 weeks travel and 2 month bills

	drop num denom
	
	
	
	//volatilities comparison
	n sum log_R_esp_er_hp l_esp_rate_hp log_R_sev_er_hp log_R_cad_er_hp log_R_sar_er_hp log_R_val_er_hp log_R_med_er_hp log_R_bar_er_hp if l_esp_rate_hp!=.
	

	
	//Non-Spanish lending rate (average of Non-spanish cities)
	egen num = rowtotal(log_R_ams_er_hp log_R_ant_er_hp log_R_gen_er_hp log_R_ham_er_hp log_R_lon_er_hp log_R_ven_er_hp), missing
	egen denom = rownonmiss(log_R_ams_er_hp log_R_ant_er_hp log_R_gen_er_hp log_R_ham_er_hp log_R_lon_er_hp log_R_ven_er_hp)
	replace denom = . if denom==0
	gen log_R_com_er_hp = num/denom
	
	drop num denom
	




//return volatilities
n sum log_R_esp_er_hp l_esp_rate_hp if year < 1780




// real rate
	gen log_cpi_sil = ln(cpi_sil)
	local det log_cpi_sil 
	include code/detrend_hp.do
	replace log_cpi_sil_hp = 100* log_cpi_sil_hp
	
// generate growth rates, lags, leads for levels and growth rates, and cumulated growth rates 
foreach r in com_er esp_er sev_er cad_er mad_er bar_er sar_er val_er med_er gen_er ams_er ant_er lon_er par_er ham_er ven_er fra_er mil_er {	
	
	gen log_R_`r'_hp_gr = log_R_`r'_hp - l.log_R_`r'_hp
	
	forvalues l = 0/`H'{
		gen l`l'_log_R_`r'_hp = l`l'.log_R_`r'_hp
		gen l`l'_log_R_`r'_hp_gr = l`l'.log_R_`r'_hp_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_log_R_`r'_hp = f`l'.log_R_`r'_hp
		gen f`l'_log_R_`r'_hp_gr = f`l'.log_R_`r'_hp_gr
		}
	forvalues h = 0/`H'{
		gen log_R_`r'_hp_gr`h'=(f`h'.log_R_`r'_hp-l.log_R_`r'_hp)
		}
	forvalues h = 2/`H'{
		gen prelog_R_`r'_hp_gr`h'=(l`h'.log_R_`r'_hp-l.log_R_`r'_hp)
		}	
		
	
	// real rates
	gen log_R_`r'_hp_r_gr = log_R_`r'_hp - l.log_R_`r'_hp - (log_cpi_sil_hp - l.log_cpi_sil_hp)
	
	forvalues l = 0/`H'{
		gen l`l'_log_R_`r'_hp_r = l`l'.log_R_`r'_hp - l`l'.log_cpi_sil_hp
		gen l`l'_log_R_`r'_hp_r_gr = l`l'.log_R_`r'_hp_r_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_log_R_`r'_hp_r = f`l'.log_R_`r'_hp - f`l'.log_cpi_sil_hp
		gen f`l'_log_R_`r'_hp_r_gr = f`l'.log_R_`r'_hp_r_gr
		}
	forvalues h = 0/`H'{
		gen log_R_`r'_hp_r_gr`h'=(f`h'.log_R_`r'_hp-l.log_R_`r'_hp) - (f`h'.log_cpi_sil_hp - l.log_cpi_sil_hp)
		}
	forvalues h = 2/`H'{
		gen prelog_R_`r'_hp_r_gr`h'=(l`h'.log_R_`r'_hp-l.log_R_`r'_hp) - (l`h'.log_cpi_sil_hp - l.log_cpi_sil_hp)
		}
		
	
	

	}

	//lower bound interest rates
	gen log_R_esp_er_hp_lo_gr = log_R_esp_er_hp_lo - l.log_R_esp_er_hp_lo
	
	forvalues l = 0/`H'{
		gen l`l'_log_R_esp_er_hp_lo = l`l'.log_R_esp_er_hp_lo
		gen l`l'_log_R_esp_er_hp_lo_gr = l`l'.log_R_esp_er_hp_lo_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_log_R_esp_er_hp_lo = f`l'.log_R_esp_er_hp_lo
		gen f`l'_log_R_esp_er_hp_lo_gr = f`l'.log_R_esp_er_hp_lo_gr
		}
	forvalues h = 0/`H'{
		gen log_R_esp_er_hp_lo_gr`h'=(f`h'.log_R_esp_er_hp_lo-l.log_R_esp_er_hp_lo)
		}
		
	//4 weeks	
	gen log_R_esp_er_hp_lo4w_gr = log_R_esp_er_hp_lo4w - l.log_R_esp_er_hp_lo4w
	
	forvalues l = 0/`H'{
		gen l`l'_log_R_esp_er_hp_lo4w = l`l'.log_R_esp_er_hp_lo4w
		gen l`l'_log_R_esp_er_hp_lo4w_gr = l`l'.log_R_esp_er_hp_lo4w_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_log_R_esp_er_hp_lo4w = f`l'.log_R_esp_er_hp_lo4w
		gen f`l'_log_R_esp_er_hp_lo4w_gr = f`l'.log_R_esp_er_hp_lo4w_gr
		}
	forvalues h = 0/`H'{
		gen log_R_esp_er_hp_lo4w_gr`h'=(f`h'.log_R_esp_er_hp_lo4w-l.log_R_esp_er_hp_lo4w)
		}
		
	
		
	//upper bound interest rates
	gen log_R_esp_er_hp_hi_gr = log_R_esp_er_hp_hi - l.log_R_esp_er_hp_hi
	
	forvalues l = 0/`H'{
		gen l`l'_log_R_esp_er_hp_hi = l`l'.log_R_esp_er_hp_hi
		gen l`l'_log_R_esp_er_hp_hi_gr = l`l'.log_R_esp_er_hp_hi_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_log_R_esp_er_hp_hi = f`l'.log_R_esp_er_hp_hi
		gen f`l'_log_R_esp_er_hp_hi_gr = f`l'.log_R_esp_er_hp_hi_gr
		}
	forvalues h = 0/`H'{
		gen log_R_esp_er_hp_hi_gr`h'=(f`h'.log_R_esp_er_hp_hi-l.log_R_esp_er_hp_hi)
		}
		
		
	gen log_R_esp_er_hp_hi4w_gr = log_R_esp_er_hp_hi4w - l.log_R_esp_er_hp_hi4w
	
	forvalues l = 0/`H'{
		gen l`l'_log_R_esp_er_hp_hi4w = l`l'.log_R_esp_er_hp_hi4w
		gen l`l'_log_R_esp_er_hp_hi4w_gr = l`l'.log_R_esp_er_hp_hi4w_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_log_R_esp_er_hp_hi4w = f`l'.log_R_esp_er_hp_hi4w
		gen f`l'_log_R_esp_er_hp_hi4w_gr = f`l'.log_R_esp_er_hp_hi4w_gr
		}
	forvalues h = 0/`H'{
		gen log_R_esp_er_hp_hi4w_gr`h'=(f`h'.log_R_esp_er_hp_hi4w-l.log_R_esp_er_hp_hi4w)
		}
		
// esp_rate:
	gen l_esp_rate_hp_gr = l_esp_rate_hp - l.l_esp_rate_hp
	
	forvalues l = 0/`H'{
		gen l`l'_l_esp_rate_hp = l`l'.l_esp_rate_hp
		gen l`l'_l_esp_rate_hp_gr = l`l'.l_esp_rate_hp_gr 
		}
	forvalues l = 1/`H'{
		gen f`l'_l_esp_rate_hp = f`l'.l_esp_rate_hp
		gen f`l'_l_esp_rate_hp_gr = f`l'.l_esp_rate_hp_gr 
		}
	forvalues h = 0/`H'{
		gen l_esp_rate_hp_gr`h'=(f`h'.l_esp_rate_hp - l.l_esp_rate_hp)
		}
	forvalues h = 2/`H'{
		gen prel_esp_rate_hp_gr`h'=(l`h'.l_esp_rate_hp-l.l_esp_rate_hp_gr)
		}	
		
 drop log_cpi_sil log_cpi_sil_hp tr_log_cpi_sil // generated again lateron






*_______________ LOGS ___________________
n display "generate logs"

local vecsize : word count `gro' 

forvalues x = 1/`vecsize' {
 local c : word `x' of `gro'
 
   gen log_`c' = ln(`c')
   *gen `c'_gr = d.`c'/L.`c'	

   	}

*_______________ DETREND ___________________
local det log_cpi_sil log_sisto_esp log_wage_sil_r
include code/detrend_hp.do

*_______________ GROWTH RATES ___________________
n display "generate growth rates"


local vecsize : word count `gro' 

forvalues x = 1/`vecsize' {
 local c : word `x' of `gro'

   //gen `c'_gr = (ln(`c')-ln(l.`c'))*100	
   gen `c'_gr = d.`c'/l.`c'*100	

   	}




*_______________ FIRST DIFFERENCES ___________________
n display "generate first differences"


local vecsize : word count `diff' 

forvalues x = 1/`vecsize' {
 local c : word `x' of `diff'
   gen `c'_gr = d.`c'
   	}






*_______________ LAGS ___________________
n display "generate lags"
local lags "`gro' percloss_salvN itsN itsN_plus pirate warbeu warbciv"

local vecsize : word count `lags' 
forvalues x = 1/`vecsize' {
 local c : word `x' of `lags'

	forvalues l=0/20 {
		gen l`l'_`c' = l`l'.`c' // lags of levels
		gen l`l'_`c'_gr = l`l'.`c'_gr // lags of growth rates
		}
		
	forvalues l=0/20 {
		gen f`l'_`c' = f`l'.`c' // leads of levels
		gen f`l'_`c'_gr = f`l'.`c'_gr // leads of growth rates
		}
   	}
	
local logs = subinstr("`gro'"," "," log_",.)

local vecsize : word count `logs' 
forvalues x = 1/`vecsize' {
 local c : word `x' of `logs'

	forvalues l=0/20 {
		gen l`l'_`c' = l`l'.`c' // lags of levels
		}
	forvalues l=0/20 {
		gen f`l'_`c' = f`l'.`c' // leads of levels
		}
   	}
	
*_______________ CUMULATED GROWTH RATES ___________________
n display "cumulated growth rates"

local vecsize : word count `cum' 

forvalues x = 1/`vecsize' {
 local c : word `x' of `cum'
	
	forvalues t = 0/`H' {
			gen `c'_gr`t' = ((f`t'.`c'-l.`c')/l.`c' )*100 
		}
	forvalues t = 2/`H' {
			gen pre`c'_gr`t' = ((l`t'.`c'-l.`c')/l.`c')*100
		}

   	}
 
 // in differences
local vecsize : word count `diff' 

forvalues x = 1/`vecsize' {
 local c : word `x' of `diff'
	
	forvalues t = 0/`H' {
			gen `c'_gr`t' = (f`t'.`c'-l.`c')			
		}
	forvalues t = 2/`H' {
			gen pre`c'_gr`t' = (l`t'.`c'-l.`c')			
		}

   	}
   	
forvalues t = 0/`H' {
		local tt = `t' -1
		if (`tt'>=0){
				replace itsN_gr`t' = (f`t'.in_esp  - f`t'.loss + f`tt'.salv - (l.in_esp - l.loss + l2.salv) )/l.sisto_esp * 100	
			}
		else {
			replace itsN_gr`t' = (f`t'.in_esp  - f`t'.loss + l.salv - (l.in_esp - l.loss + l2.salv) )/l.sisto_esp * 100	
		}	
		}
		
forvalues t = 0/`H' {
		gen itsN_gr_SE`t' = f`t'.itsN - l.itsN
}

forvalues t = 2/`H' {
	local tt = `t' +1
			replace pre`c'_gr`t' =  (l`t'.in_esp - l`t'.loss + l`tt'.salv - (l.in_esp - l.loss + l2.salv) )/l.sisto_esp * 100			
		}

	
		


*_____________________________________________________________________


*sum
n display "save datasets"

* save prepared .dta file
keep if year <= 1810 & year >= 1531
save "data/working files/prepared.dta", replace



} //quiet


